package com.ws.universal.tools.utils;

import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.IllegalFormatException;

/**
 * @创建者 ws
 * @TIME 2021/8/31 16:16
 * @描述
 */
public class StringUtils {

    public static StringBuilder sp = new StringBuilder();

    public static String getString(String... arg) {
        sp.setLength(0);
        for (int i = 0; i < arg.length; i++) {
            sp.append(arg[i]);
        }
        return sp.toString();
    }

    public static String getString(int str) {
        sp.setLength(0);
        sp.append(str);
        return sp.toString();
    }

    public static String getString(Object... str) {
        sp.setLength(0);
        for (int i = 0; i < str.length; i++) {
            sp.append(str[i]);
        }
        return sp.toString();
    }

    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }

    public static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Return whether string1 is equals to string2.
     *
     * @param s1 The first string.
     * @param s2 The second string.
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean equals(final CharSequence s1, final CharSequence s2) {
        if (s1 == s2) return true;
        int length;
        if (s1 != null && s2 != null && (length = s1.length()) == s2.length()) {
            if (s1 instanceof String && s2 instanceof String) {
                return s1.equals(s2);
            } else {
                for (int i = 0; i < length; i++) {
                    if (s1.charAt(i) != s2.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * Format the string.
     *
     * @param str  The string.
     * @param args The args.
     * @return a formatted string.
     */
    public static String format(@Nullable String str, Object... args) {
        String text = str;
        if (text != null) {
            if (args != null && args.length > 0) {
                try {
                    text = String.format(str, args);
                } catch (IllegalFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return text;
    }

    public static void generateSp(TextView tv, String str, boolean isUnderline, int color1, int color2, PublicTextCallback callback, String str1, String str2) {
        SpannableString sp = new SpannableString(str);
        UnderlineSpan underlineSpan = new UnderlineSpan();
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color1);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(color2);
        int start = 0, end;

        start = str.indexOf(str1);
        end = str.indexOf(str1) + str1.length();

        sp.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                callback.onProtocolOnClick(0);
            }
        }, start, end, 0);
        if (isUnderline) {
            sp.setSpan(underlineSpan, start, end, 0);
        }

        sp.setSpan(colorSpan, start, end, 0);

        SpannableString sp2 = new SpannableString(sp);
        start = 0;
        start = str.indexOf(str2);
        end = str.indexOf(str2) + str2.length();
        sp2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                callback.onProtocolOnClick(0);
            }
        }, start, end, 0);
        if (isUnderline) {
            sp2.setSpan(underlineSpan, start, end, 0);
        }
        sp2.setSpan(colorSpan2, start, end, 0);
        tv.setText(sp2);
        tv.setMovementMethod(LinkMovementMethod.getInstance());

    }


    public static void generateSp(TextView tv, String str, boolean isUnderline, int color1, PublicTextCallback callback, String str1) {
        SpannableString sp = new SpannableString(str);
        UnderlineSpan underlineSpan = new UnderlineSpan();
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color1);
        int start = 0, end;
        start = str.indexOf(str1);
        end = str.indexOf(str1) + str1.length();

        sp.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                callback.onProtocolOnClick(0);
            }
        }, start, end, 0);
        if (isUnderline) {
            sp.setSpan(underlineSpan, start, end, 0);
        }
        sp.setSpan(colorSpan, start, end, 0);
        tv.setText(sp);
        tv.setMovementMethod(LinkMovementMethod.getInstance());

    }

    public static String toUpperTitle(final String s) {
        String[] a = s.split(" ");
        for (int i = 0; i < a.length; i++) {
            String b = a[i].substring(0, 1);
            String c = a[i].replaceFirst(b, b.toUpperCase());
            a[i] = c;
        }
        sp.setLength(0);
        for (int i = 0; i < a.length; i++) {
            if (i == a.length - 1) {
                sp.append(a[i]);
            } else {
                sp.append(a[i] + " ");
            }

        }
        return sp.toString();

    }

    public interface PublicTextCallback {
        void onProtocolOnClick(int index);

    }
}
